package com.mikepenz.presenter.imp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.mikepenz.bean.CarBean;
import com.mikepenz.interactor.SendOrderInteractor;
import com.mikepenz.interactor.imp.SendOrderInteractorImp;
import com.mikepenz.presenter.SendOrderPresenter;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.CookicUntil;
import com.mikepenz.utils.SystemUtils;
import com.mikepenz.view.SendOrderView;
import com.quickdv.activity.BaseActivity;

/**
 * Created by lilongfei on 15/9/21.
 */
public class SendOrderPresenterImp implements SendOrderPresenter ,RequestListener<JSONObject>{

    SendOrderInteractor interactor;
    SendOrderView view;
    Context c;
    CarBean car;

    int timeType = -1;

    public SendOrderPresenterImp (Context c){
        this(c,(SendOrderView)c);
    }

    public SendOrderPresenterImp(Context c,SendOrderView view){
        this.c =c;
        this.view =view;
        interactor = new SendOrderInteractorImp(c,this);

        Bundle bundle = ((Activity)c).getIntent().getExtras();
        if(bundle !=null) car = (CarBean) bundle.getSerializable("car");
    }

    @Override
    public boolean checkText(String text) {
        if (text.length()>45) {
            view.setTextError("长度不可超过50字符");
            return false;
        }
        return true;
    }

    @Override
    public void submit(int sendType, String startTime, String endTime,String description) {
        String userId = CookicUntil.getUser().getUserId();
        String cardId = CookicUntil.getCardBean().getCardId();
        if (userId == null){
            view.showError("用户未登陆");
            return;
        }

        if (sendType == -1){
            view.showError("没有设置租用类型");
            return;
        }
        if (sendType != 1 && (startTime == null || startTime.length() ==0 || endTime == null || endTime.length()==0)){
            view.showError("租用时间不可为空");
            return;
        }
        if (car ==null) return;
        if (cardId ==null ) return;

        String currentTime = SystemUtils.getCurrentTime();
        interactor.sendOrder(userId,cardId,car.getCarId(),car.getCarType(),0,sendType,currentTime,startTime,endTime);
        view.showAlert();
    }

    @Override
    public void checkTime(int type) {
        this.timeType = type;
    }

    @Override
    public void bindTime(String time) {
        if (timeType == 0){
            view.setStartTime(time);
        }else if (timeType == 1){
            view.setEndTime(time);
        }
    }

    @Override
    public void Success(String tag, JSONObject result) {
        view.dismissAlert();
        if (result!=null){
            String orderId = result.getString("orderId");
        }
        ((BaseActivity)c).finish();
    }

    @Override
    public void Error(String tag, String error) {
        view.dismissAlert();
        view.showError(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissAlert();
        view.showError("服务器异常，请稍后重试");
    }
}
